package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.dao.*;
import com.HospitalOutpatient.entity.*;
import com.HospitalOutpatient.service.ReceptionService;
import com.HospitalOutpatient.utils.DateUtil;
import com.HospitalOutpatient.vo.PatientAndReceptionVo;
import com.HospitalOutpatient.vo.ReceptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("receptionService")
public class ReceptionServiceImpl implements ReceptionService {

    @Autowired
    private ReceptionDao receptionDao;
    @Autowired
    private DutyDao dutyDao;
    @Autowired
    private PatientDao patientDao;
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private OutpatientDao outpatientDao;

    @Override
    public boolean addReception(Reception reception) {
        Duty duty = dutyDao.selectDutyById(reception.getDutyId());
        if(Integer.parseInt(duty.getPartialNum()) <= 0){
            return false;
        }
        if(dutyDao.updateDutyById(duty.getDutyId()) != 1){
            return false;
        }
        return receptionDao.insertReception(reception) >= 1;
    }

    @Override
    public List<ReceptionVo> getReceptionByUserId(String userId) {
        List<Patient> patientList = patientDao.selectPatientByuserId(userId);

        List<ReceptionVo> receptionVoList = new ArrayList<>();
        for (Patient patient : patientList) {


            List<Reception> receptionList = receptionDao.selectReceptionByPatientId(patient.getPatientId());
            for (Reception reception : receptionList) {

                Duty duty = dutyDao.selectDutyById(reception.getDutyId());

                String dateTime = duty.getStartTime()+" ~ "+duty.getEndTime();

                Doctor doctor = doctorDao.selectDoctorById(duty.getDoctorId());
                Outpatient outpatient = outpatientDao.getOutpatientById(duty.getOutpatientId());

                ReceptionVo receptionVo = new ReceptionVo();
                receptionVo.setDoctorName(doctor.getDoctorName());
                receptionVo.setOutpatientName(outpatient.getOutpatientName());
                receptionVo.setPatientName(patient.getPatientName());
                receptionVo.setDateTime(dateTime);
                receptionVo.setReceptionId(reception.getReceptionId());

                receptionVoList.add(receptionVo);
            }
        }
        return receptionVoList;
    }

    @Override
    public List<PatientAndReceptionVo> getTodayReception(String doctorId,String patientName) {

        List<Duty> dutyList = dutyDao.selectDutyByDoctorIdAndDate(doctorId);

        List<PatientAndReceptionVo> patientAndReceptionVoList = new ArrayList<>();
        for (Duty duty : dutyList) {
            List<Reception> receptionList = receptionDao.selectReceptionBydutyId(duty.getDutyId());
            String dateTime = duty.getStartTime()+" ~ "+duty.getEndTime();

            for (Reception reception : receptionList) {

                Patient patient = patientDao.selectPatientById(reception.getPatientId());

                if(patientName!=null && !patientName.equals("")){
                    if(!patient.getPatientName().contains(patientName)){
                        continue;
                    }
                }

                PatientAndReceptionVo patientAndReceptionVo = new PatientAndReceptionVo();
                patientAndReceptionVo.setReceptionId(reception.getReceptionId());
                patientAndReceptionVo.setPatientName(patient.getPatientName());
                patientAndReceptionVo.setIdCard(patient.getIdCard());
                patientAndReceptionVo.setDateTime(dateTime);

                patientAndReceptionVoList.add(patientAndReceptionVo);
            }
        }
        return patientAndReceptionVoList;
    }

    @Override
    public List<ReceptionVo> getNewReceptionByUserId(String userId) {

        List<Patient> patientList = patientDao.selectPatientByuserId(userId);

        List<ReceptionVo> receptionVoList = new ArrayList<>();
        for (Patient patient : patientList) {


            List<Reception> receptionList = receptionDao.selectReceptionByPatientId(patient.getPatientId());
            for (Reception reception : receptionList) {
                if(reception.getDiagnosisId() != null){
                    continue;
                }

                Duty duty = dutyDao.selectDutyById(reception.getDutyId());
                Date endTime = new Date();
                try {
                    endTime = DateUtil.StringToDateTime(duty.getEndTime());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                Calendar calendar = Calendar.getInstance();
                Date now = calendar.getTime();


                int result = endTime.compareTo(now);
                if(result < 0){
                    continue;
                }

                String dateTime = duty.getStartTime()+" ~ "+duty.getEndTime();
                Doctor doctor = doctorDao.selectDoctorById(duty.getDoctorId());
                Outpatient outpatient = outpatientDao.getOutpatientById(duty.getOutpatientId());

                ReceptionVo receptionVo = new ReceptionVo();
                receptionVo.setDoctorName(doctor.getDoctorName());
                receptionVo.setOutpatientName(outpatient.getOutpatientName());
                receptionVo.setPatientName(patient.getPatientName());
                receptionVo.setDateTime(dateTime);
                receptionVo.setReceptionId(reception.getReceptionId());

                receptionVoList.add(receptionVo);
            }
        }
        return receptionVoList;
    }

    @Override
    public boolean deleteReceptionById(String receptionId) {

        Reception reception = receptionDao.selectReceptionById(receptionId);
        Duty duty = dutyDao.selectDutyById(reception.getDutyId());
        Date today = DateUtil.getYearMonthDay();
        Date dutyDate;
        try {
            dutyDate = DateUtil.StringToDate(duty.getDutyDate());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        int result = dutyDate.compareTo(today);
        if(result > 0){
            int i = receptionDao.deleteReceptionById(receptionId);
            int i1 = dutyDao.updateAddDutyById(reception.getDutyId());
            return i + i1 == 2;
        }
        return false;
    }

    @Override
    public List<PatientAndReceptionVo> getFinishReception(String doctorId, String patientName) {
        List<Duty> dutyList = dutyDao.selectFinishDutyByDoctorId(doctorId);
        List<PatientAndReceptionVo> patientAndReceptionVoList = new ArrayList<>();
        for (Duty duty : dutyList) {
            List<Reception> receptionList = receptionDao.selectFinishReceptionBydutyId(duty.getDutyId());
            String dateTime = duty.getStartTime()+" ~ "+duty.getEndTime();

            for (Reception reception : receptionList) {
                if(reception.getDiagnosisId() == null){
                    continue;
                }

                Patient patient = patientDao.selectPatientById(reception.getPatientId());

                if(patientName!=null && !patientName.equals("")){
                    if(!patient.getPatientName().contains(patientName)){
                        continue;
                    }
                }

                PatientAndReceptionVo patientAndReceptionVo = new PatientAndReceptionVo();
                patientAndReceptionVo.setReceptionId(reception.getReceptionId());
                patientAndReceptionVo.setPatientName(patient.getPatientName());
                patientAndReceptionVo.setIdCard(patient.getIdCard());
                patientAndReceptionVo.setDateTime(dateTime);

                patientAndReceptionVoList.add(patientAndReceptionVo);
            }
        }
        return patientAndReceptionVoList;
    }
}
